using System;
using System.Collections.Generic;
using System.Text;

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

using HadronData;

namespace Hadron.Entities
{
    class Drone
        : Entity
    {
        private static Polygon s_polygonGeometry;
        private Polygon m_polygonGeometry;
        private float m_fRetargetTime;
        private Vector2 m_vecTarget;

        public Drone(EntityMarker p_entityMarker, Environment p_environment)
            : base(p_entityMarker, p_environment)
        {
            // construct geometry
            if (s_polygonGeometry == null)
            {
                s_polygonGeometry = new Polygon();
                float fAngleDelta = MathHelper.TwoPi / 8.0f;
                for (int nIndex = 0; nIndex < 8; nIndex++)
                {
                    float fAngle = fAngleDelta * nIndex;
                    Vector2 vecVertexInner0 = new Vector2((float) Math.Cos(fAngle) * 8.0f, (float) Math.Sin(fAngle) * 8.0f);
                    Vector2 vecVertexInner1 = new Vector2((float)Math.Cos(fAngle + fAngleDelta) * 4.0f, (float)Math.Sin(fAngle + fAngleDelta) * 4.0f);
                    Vector2 vecVertexOuter = vecVertexInner0 * 1.5f;
                    s_polygonGeometry.Edges.Add(new Edge(vecVertexInner0, vecVertexOuter, Color.YellowGreen));
                    s_polygonGeometry.Edges.Add(new Edge(vecVertexOuter, vecVertexInner1, Color.YellowGreen));
                    s_polygonGeometry.UpdateBounds();
                }
            }

            m_nHealth = 3;
            m_nPoints = 300;

            m_polygonGeometry = new Polygon(s_polygonGeometry);
            m_polygonGeometry.Transform(m_trnPlacement);

            m_circleBounds = s_polygonGeometry.GetBoundingCircle(Vector2.Zero);
        }

        public override void Update(GameTime p_gameTime)
        {
            float fDeltaTime = (float)p_gameTime.ElapsedGameTime.TotalSeconds;

            m_trnPlacement.Rotation += fDeltaTime * MathHelper.Pi;

            m_fRetargetTime -= fDeltaTime;

            if (m_fRetargetTime <= 0.0f)
            {
                m_vecTarget = m_environment.PlayerShip.Position;
                m_vecTarget.X += ((float)s_random.NextDouble() - 0.5f) * 256.0f;
                m_vecTarget.Y += ((float)s_random.NextDouble() - 0.5f) * 256.0f;
                m_fRetargetTime = (float)s_random.NextDouble() * 5.0f;
            }

            Vector2 vecDirection = m_vecTarget - m_trnPlacement.Translation;
            vecDirection.Normalize();
            m_vecVelocity = vecDirection * 32.0f;

            m_trnPlacement.Translation += m_vecVelocity * fDeltaTime;

            m_circleBounds.Centre = m_trnPlacement.Translation;

            m_polygonGeometry.Transform(m_trnPlacement, s_polygonGeometry);

            // collisions with level
            Level level = m_environment.CurrentLevel;
            List<ContactPoint> listContactPoints = new List<ContactPoint>();
            level.ComputeContactPoints(m_polygonGeometry, listContactPoints);
            foreach (ContactPoint contactPoint in listContactPoints)
                m_trnPlacement.Translation += contactPoint.Normal * 0.5f;
        }

        public override void Draw(SpriteBatch p_spriteBatch)
        {
            m_polygonGeometry.Draw(p_spriteBatch);
        }

        public override EntityType Type
        {
            get { return EntityType.Drone; }
        }

        public override bool Destructuble
        {
            get { return true; }
        }

        public override bool Hostile
        {
            get { return true; }
        }

        public override Geometry Geometry
        {
            get { return m_polygonGeometry; }
        }
    }
}
